// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-02-11

package binlog

import "fmt"

// Request
// 来自 Binlog 的消息结构.
//
//   {
//       "data": [
//           {
//               "name": "replication_group_member_actions",
//               "version": 2
//           }
//       ],
//       "database": "mysql",
//       "es": 1739243447000,
//       "gtid": "",
//       "id": 2,
//       "isDdl": false,
//       "mysqlTypes": {
//       },
//       "old": [
//           {
//               "version": 1
//           }
//       ],
//       "pkNames": null,
//       "sqlType": "",
//       "table": "replication_group_configuration_version",
//       "ts": 1739243447325,
//       "type": "UPDATE"
//   }
type Request struct {
    Data     []map[string]any `json:"data" label:"数据列表"`            // 批量模式(例如事务操作多条数据时)
    Database string           `json:"database" label:"数据库名"`        // 数据来自哪个库
    Old      []map[string]any `json:"old,omitempty" label:"修改前数据"` // 若 DML 为 UPDATE 时有值
    Table    string           `json:"table" label:"数据表名"`           // 数据来自哪个表
    Type     string           `json:"type" label:"操作类型"`            // 数据库 DML 中的 DELETE, INSERT, UPDATE 三大类写操作.
}

func (o *Request) TableKey() string { return fmt.Sprintf(`%s.%s`, o.Database, o.Table) }
